package org.kjtc.controller;

import net.sf.json.JSONObject;
import org.kjtc.entity.WechatUser;
import org.kjtc.service.WeChartService;
import org.kjtc.weChatUtil.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping
public class FeedbackController {

    @Autowired
    private WeChartService weChartService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @RequestMapping(value = "/sendFeedback")
    @ResponseBody
    public Object sendFeedback(String user, String phone, String message) {

        Map<String, Object> map = new HashMap<>();
        List<String> keywordList = new ArrayList<String>();
        try {
            map.put("result", "Fail");
            JSONObject mobileJson = CommonUtil.mobileLocation(phone);
            if (mobileJson.containsKey("province") && mobileJson.containsKey("catName")) {
                map.put("mobileInfo", "(" + mobileJson.getString("province")+" "+mobileJson.getString("catName") + ")");

                keywordList.add(user);
                keywordList.add(CommonUtil.date2Str(new Date(), null));
                if (message.indexOf("\n") >= 0) {
                    message = message.replace("\n", "\\n");
                }
                keywordList.add("\\n" + "\\n" + message + "\\n用户电话：\\n" + phone + map.get("mobileInfo") + "\\n");
                Object result = CommonUtil.sendWechatMessage("feedback", keywordList, weChartService, redisTemplate);
                if (result != null) {
                    map = (Map) result;
                    if (map.containsKey("result") && map.get("result") != null) {
                       if(map.get("result").toString().indexOf("success") >= 0) {
                           map.put("result", "Success");
                       }
                    }
                }
            } else {
                map.put("result", "MobileError");
            }
        } catch (Exception e) {
            map.put("result", "Error");
        }
        return map;
    }

    @RequestMapping(value = "/feedback")
    public String feedback(String userID, String openID, Model model) {
        if (CommonUtil.test_model) {
            userID = "dt";
        }
        if ((userID != null && !userID.isEmpty()) || (openID != null && !openID.isEmpty())) {
            WechatUser wechatUser = new WechatUser();
            if ((userID != null && !userID.isEmpty())) {
                wechatUser.setUserID("'" + userID + "'");
            }
            wechatUser.setWeChatID(openID);
            List<WechatUser> list = weChartService.searchWechatUser(wechatUser);
            if (list.size() > 0) {
                wechatUser = list.get(0);
                if(wechatUser.getUserName() != null && !wechatUser.getUserName().isEmpty()){
                    model.addAttribute("userName", wechatUser.getUserName());
                } else {
                    model.addAttribute("userName", wechatUser.getWeChatNickName());
                }
                if(wechatUser.getMobile() != null && !wechatUser.getMobile().isEmpty() && wechatUser.getMobile().indexOf("-") < 0){
                    model.addAttribute("mobile", wechatUser.getMobile());
                }
            }
            return "weChatApp/feedback/chargingFeedback";
        } else {
//            return "weChatApp/feedback/chargingFeedback";
            return "error/errorPage";
        }
    }

    @RequestMapping(value = "/feedbackResult")
    public String feedbackResult() {

        return "weChatApp/feedback/feedbackResult";
    }
}
